package com.xfcycle.zebrabikegp.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.xfcycle.zebrabikegp.listener.SharePreferencesDeleteDataListener;

import java.util.Map;
import java.util.Set;

public class HandleDataBySharePreferencesUtil {
    private SharedPreferences sp;
    private Context context;
    private SharePreferencesDeleteDataListener sharePreferencesDeleteDataListener;

    private static HandleDataBySharePreferencesUtil instance;

    private HandleDataBySharePreferencesUtil(Context context) {
        this.context = context;
    }

    public static HandleDataBySharePreferencesUtil getInstance(Context context) {
        synchronized (HandleDataBySharePreferencesUtil.class) {
            if (null == instance || !context.equals(instance.context)) {
                instance = new HandleDataBySharePreferencesUtil(context);
            }
        }
        return instance;
    }

    /**
     * 从SharePreferences中获取数据
     *
     * @param spName
     * @param key
     * @return 得到的数据
     */
    public String getDateFromSharePre(String spName, String key) {
        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        if (null == sp) {
            return null;
        }
        return sp.getString(key, null);
    }

    /**
     * 将数据放入到SharePreferences中
     *
     * @param spName
     * @param datas
     */
    public void putDateIntoSharePre(final String spName, final Map<String, String> datas) {
        new Thread() {
            @Override
            public void run() {
                sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
                Editor editor = sp.edit();
                if (null != datas) {
                    for (Map.Entry<String, String> data : datas.entrySet()) {
                        editor.putString(data.getKey(), data.getValue());
                    }
                }
                editor.commit();
            }
        }.start();
    }

    /**
     * 删除SharePreferences中的特定数据
     *
     * @param spName
     * @param keys
     */
    public void deleteTargetDataInSharePre(final String spName, final Set<String> keys) {
        new Thread() {
            @Override
            public void run() {
                sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
                Editor editor = sp.edit();
                if (null != keys && !keys.isEmpty()) {
                    for (String key : keys) {
                        editor.remove(key);
                    }
                }
                editor.commit();

                sharePreferencesDeleteDataListener.sharePreferencesDeleteDataFinish();
            }
        }.start();
    }

    /**
     * 删除SharePreferences中的所有数据
     *
     * @param spName sp名称
     */
    public void deleteAllDataInSharePre(final String spName) {
        new Thread() {
            @Override
            public void run() {
                sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
                Editor editor = sp.edit();
                editor.clear();
                editor.commit();
                sharePreferencesDeleteDataListener.sharePreferencesDeleteDataFinish();
            }
        }.start();
    }

    /**
     * 删除SharePreferences中的数据 完成监听器
     * @param sharePreferencesDeleteDataListener 自定义监听器
     */
    public void onDeleteDataInSharePreListener(SharePreferencesDeleteDataListener sharePreferencesDeleteDataListener){
        this.sharePreferencesDeleteDataListener = sharePreferencesDeleteDataListener;
    }
}
